package com.ruoyi.type.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.type.domain.Animation;
import com.ruoyi.type.service.IAnimationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动漫Controller
 *
 * @author ucu
 * @date 2024-05-04
 */
@RestController
@RequestMapping("/type/animation")
public class AnimationController extends BaseController {
    @Autowired
    private IAnimationService animationService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询动漫列表
     */
    @PreAuthorize("@ss.hasPermi('type:animation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Animation animation) {
        startPage();
        List<Animation> list = animationService.selectAnimationList(animation);
        return getDataTable(list);
    }

    /**
     * 导出动漫列表
     */
    @PreAuthorize("@ss.hasPermi('type:animation:export')")
    @Log(title = "动漫", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Animation animation) {
        List<Animation> list = animationService.selectAnimationList(animation);
        ExcelUtil<Animation> util = new ExcelUtil<Animation>(Animation.class);
        util.exportExcel(response, list, "动漫数据");
    }

    /**
     * 获取动漫详细信息
     */
    @PreAuthorize("@ss.hasPermi('type:animation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(animationService.selectAnimationById(id));
    }

    /**
     * 新增动漫
     */
    @PreAuthorize("@ss.hasPermi('type:animation:add')")
    @Log(title = "动漫", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Animation animation) {
        if (!("true".equals(configService.selectConfigByKey("info.image.upload")))) {
            animation.setPicture(null);
        }
        if (!("true".equals(configService.selectConfigByKey("info.image.url")))) {
            animation.setPicture(null);
        }

        animation.setReTime(new Date());
        return toAjax(animationService.insertAnimation(animation));
    }

    /**
     * 修改动漫
     */
    @PreAuthorize("@ss.hasPermi('type:animation:edit')")
    @Log(title = "动漫", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Animation animation) {
        if (!("true".equals(configService.selectConfigByKey("info.image.upload")))) {
            animation.setPicture(null);
        }
        if (!("true".equals(configService.selectConfigByKey("info.image.url")))) {
            animation.setPicture(null);
        }
        animation.setReTime(new Date());
        return toAjax(animationService.updateAnimation(animation));
    }

    /**
     * 删除动漫
     */
    @PreAuthorize("@ss.hasPermi('type:animation:remove')")
    @Log(title = "动漫", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(animationService.deleteAnimationByIds(ids));
    }
}
